<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class Setting extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();	
		$this->template->set_template('template/admin_template');
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	}
	function index()
	{
        $data['breadcrumb'] = set_crumbs(array(current_url() => 'General Settings'));
		//$data['pages'] = $this->general_model->select_rows('pages','*','title','asc','10');
		$data['pages'] = $this->general_model->select("pages","pageid,title","status = 'published'");
		$data['settings']=$this->general_model->select_val("setting","*","id = 1");
		if($_POST)
		{
			$paypal=array(
				"paypal_email"=>$this->input->post('paypal_email'),
				"sandbox_mode"=>$this->input->post('sandbox_mode'),
				"status"=>$this->input->post('status')
			);
			$settingdata=array("site_name"=>$this->input->post('site_name'),
								"notification_email"=>$this->input->post('notification_email'),
								"site_homepage"=>$this->input->post('site_homepage'),
								"facebook"=>$this->input->post('facebook'),
								"twitter"=>$this->input->post('twitter'),
								"youtube"=>$this->input->post('youtube'),
								"paypal"=>json_encode($paypal),
								"site_logo"=>$this->input->post('site_logo'),
								"site_banner"=>$this->input->post('site_banner'),
								"countdown_date"=>date("Y-m-d H:i:s",strtotime($this->input->post('countdown_date')))
								);
			$this->general_model->update("setting",$settingdata,"id = 1");
			$updatedata=array("is_home"=>1);
			$noupdatedata=array("is_home"=>0);
			$this->general_model->update("pages",$updatedata,"pageid =".$this->input->post('site_homepage'));
			$this->general_model->update("pages",$noupdatedata,"pageid !=".$this->input->post('site_homepage'));
			$this->session->set_flashdata('message', '<p class="success">Setting added Saved.</p>');
			redirect(ADMIN_PATH . "/setting");
		}
		$this->template->content->view('admin/system/general_setting', $data);
		$this->template->publish();
    }
	public function countries()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Countires'));
		$data['totalcountries']=$this->general_model->select("country");
		$count=count($data['totalcountries']);
		$perpage = 10;
		if ($count > $perpage) {
			$this->load->library('pagination');
		    $data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';
			// Create Pagination
			$config['base_url'] = site_url(ADMIN_PATH . '/setting/countries/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage; 
			$config['uri_segment'] = '4';
			$config['num_links'] = 4 ;
			$config['full_tag_open'] = false;
			$config['full_tag_close'] = false;
			$config['cur_tag_open'] = '<a href="#">';
			$config['cur_tag_close'] = '</a>';
			$config['next_tag_open'] = false;
			$config['next_tag_close'] = false;
			$config['prev_tag_open'] = false;
			$config['prev_tag_close'] = false;
			$config['num_tag_open'] = false;
			$config['num_tag_close'] = false;
			$config['suffix'] = $data['query_string'];
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(4);
		}
		$data['countries'] = $this->general_model->select_rows("country","*","country_id","asc",$perpage,$offset);
		$this->template->content->view('admin/system/countries', $data);
		$this->template->publish();
	}
	public function addcountry()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/countries' => 'All Country', current_url() => 'Add Country'));
		$this->form_validation->set_rules('name', 'Country Name', 'required');
        $this->form_validation->set_rules('iso_code_3', 'ISO Code', 'trim|required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$countrydata=array(
					"name"=>$this->input->post('name'),
					"iso_code_3"=>$this->input->post('iso_code_3'),
					"phone_code"=>$this->input->post('phone_code'),
					"status"=>$this->input->post('status'),
				);
				$countryid=$this->general_model->insert('country',$countrydata);
				$this->session->set_flashdata('message', '<p class="success">country added successfully</p>');
				redirect(ADMIN_PATH . '/setting/countries');
			}
		$this->template->content->view('admin/system/addcountry', $data);
		$this->template->publish();
	}
	public function editcountry()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/countries' => 'All Countries', current_url() => 'Edit Country'));
		$countryid=$this->uri->segment(4);
		$this->form_validation->set_rules('name', 'Country Name', 'required');
        $this->form_validation->set_rules('iso_code_3', 'ISO Code', 'trim|required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$countrydata=array(
					"name"=>$this->input->post('name'),
					"iso_code_3"=>$this->input->post('iso_code_3'),
					"phone_code"=>$this->input->post('phone_code'),
					"status"=>$this->input->post('status'),
				);
				$this->general_model->update('country',$countrydata,"country_id = ".$countryid);
				$this->session->set_flashdata('message', '<p class="success">Practice updated successfully</p>');
				redirect(ADMIN_PATH . '/setting/countries');
			}
		$data['country']=$this->general_model->select_val("country","*","country_id = ".$countryid);
		$this->template->content->view('admin/system/editcountry',$data);
		$this->template->publish();
	}
	public function updatecountry()
	{
		$countries=$this->general_model->select("country");
		$countryCallingCodes = array
		(
			93 => "Afghanistan",
			355 => "Albania",
			213 => "Algeria",
			1 => "American Samoa",
			376 => "Andorra",
			244 => "Angola",
			1 => "Anguilla",
			1 => "Antigua and Barbuda",
			54 => "Argentina",
			374 => "Armenia",
			297 => "Aruba",
			247 => "Ascension",
			61 => "Australia",
			43 => "Austria",
			994 => "Azerbaijan",
			1 => "Bahamas",
			973 => "Bahrain",
			880 => "Bangladesh",
			1 => "Barbados",
			375 => "Belarus",
			32 => "Belgium",
			501 => "Belize",
			229 => "Benin",
			1 => "Bermuda",
			975 => "Bhutan",
			591 => "Bolivia",
			387 => "Bosnia and Herzegovina",
			267 => "Botswana",
			55 => "Brazil",
			1 => "British Virgin Islands",
			673 => "Brunei",
			359 => "Bulgaria",
			226 => "Burkina Faso",
			257 => "Burundi",
			855 => "Cambodia",
			237 => "Cameroon",
			1 => "Canada",
			238 => "Cape Verde",
			1 => "Cayman Islands",
			236 => "Central African Republic",
			235 => "Chad",
			56 => "Chile",
			86 => "China",
			57 => "Colombia",
			269 => "Comoros",
			242 => "Congo",
			682 => "Cook Islands",
			506 => "Costa Rica",
			385 => "Croatia",
			53 => "Cuba",
			357 => "Cyprus",
			420 => "Czech Republic",
			243 => "Democratic Republic of Congo",
			45 => "Denmark",
			246 => "Diego Garcia",
			253 => "Djibouti",
			1 => "Dominica",
			1 => "Dominican Republic",
			670 => "East Timor",
			593 => "Ecuador",
			20 => "Egypt",
			503 => "El Salvador",
			240 => "Equatorial Guinea",
			291 => "Eritrea",
			372 => "Estonia",
			251 => "Ethiopia",
			500 => "Falkland (Malvinas) Islands",
			298 => "Faroe Islands",
			679 => "Fiji",
			358 => "Finland",
			33 => "France",
			594 => "French Guiana",
			689 => "French Polynesia",
			241 => "Gabon",
			220 => "Gambia",
			995 => "Georgia",
			49 => "Germany",
			233 => "Ghana",
			350 => "Gibraltar",
			30 => "Greece",
			299 => "Greenland",
			1 => "Grenada",
			590 => "Guadeloupe",
			1 => "Guam",
			502 => "Guatemala",
			224 => "Guinea",
			245 => "Guinea-Bissau",
			592 => "Guyana",
			509 => "Haiti",
			504 => "Honduras",
			852 => "Hong Kong",
			36 => "Hungary",
			354 => "Iceland",
			91 => "India",
			62 => "Indonesia",
			870 => "Inmarsat Satellite",
			98 => "Iran",
			964 => "Iraq",
			353 => "Ireland",
			972 => "Israel",
			39 => "Italy",
			225 => "Ivory Coast",
			1 => "Jamaica",
			81 => "Japan",
			962 => "Jordan",
			7 => "Kazakhstan",
			254 => "Kenya",
			686 => "Kiribati",
			965 => "Kuwait",
			996 => "Kyrgyzstan",
			856 => "Laos",
			371 => "Latvia",
			961 => "Lebanon",
			266 => "Lesotho",
			231 => "Liberia",
			218 => "Libya",
			423 => "Liechtenstein",
			370 => "Lithuania",
			352 => "Luxembourg",
			853 => "Macau",
			389 => "Macedonia",
			261 => "Madagascar",
			265 => "Malawi",
			60 => "Malaysia",
			960 => "Maldives",
			223 => "Mali",
			356 => "Malta",
			692 => "Marshall Islands",
			596 => "Martinique",
			222 => "Mauritania",
			230 => "Mauritius",
			262 => "Mayotte",
			52 => "Mexico",
			691 => "Micronesia",
			373 => "Moldova",
			377 => "Monaco",
			976 => "Mongolia",
			382 => "Montenegro",
			1 => "Montserrat",
			212 => "Morocco",
			258 => "Mozambique",
			95 => "Myanmar",
			264 => "Namibia",
			674 => "Nauru",
			977 => "Nepal",
			31 => "Netherlands",
			599 => "Netherlands Antilles",
			687 => "New Caledonia",
			64 => "New Zealand",
			505 => "Nicaragua",
			227 => "Niger",
			234 => "Nigeria",
			683 => "Niue Island",
			850 => "North Korea",
			1 => "Northern Marianas",
			47 => "Norway",
			968 => "Oman",
			92 => "Pakistan",
			680 => "Palau",
			507 => "Panama",
			675 => "Papua New Guinea",
			595 => "Paraguay",
			51 => "Peru",
			63 => "Philippines",
			48 => "Poland",
			351 => "Portugal",
			1 => "Puerto Rico",
			974 => "Qatar",
			262 => "Reunion",
			40 => "Romania",
			7 => "Russian Federation",
			250 => "Rwanda",
			290 => "Saint Helena",
			1 => "Saint Kitts and Nevis",
			1 => "Saint Lucia",
			508 => "Saint Pierre and Miquelon",
			1 => "Saint Vincent and the Grenadines",
			685 => "Samoa",
			378 => "San Marino",
			239 => "Sao Tome and Principe",
			966 => "Saudi Arabia",
			221 => "Senegal",
			381 => "Serbia",
			248 => "Seychelles",
			232 => "Sierra Leone",
			65 => "Singapore",
			421 => "Slovakia",
			386 => "Slovenia",
			677 => "Solomon Islands",
			252 => "Somalia",
			27 => "South Africa",
			82 => "South Korea",
			34 => "Spain",
			94 => "Sri Lanka",
			249 => "Sudan",
			597 => "Suriname",
			268 => "Swaziland",
			46 => "Sweden",
			41 => "Switzerland",
			963 => "Syria",
			886 => "Taiwan",
			992 => "Tajikistan",
			255 => "Tanzania",
			66 => "Thailand",
			228 => "Togo",
			690 => "Tokelau",
			1 => "Trinidad and Tobago",
			216 => "Tunisia",
			90 => "Turkey",
			993 => "Turkmenistan",
			1 => "Turks and Caicos Islands",
			688 => "Tuvalu",
			256 => "Uganda",
			380 => "Ukraine",
			971 => "United Arab Emirates",
			44 => "United Kingdom",
			1 => "United States of America",
			1 => "U.S. Virgin Islands",
			598 => "Uruguay",
			998 => "Uzbekistan",
			678 => "Vanuatu",
			379 => "Vatican City",
			58 => "Venezuela",
			84 => "Vietnam",
			681 => "Wallis and Futuna",
			967 => "Yemen",
			260 => "Zambia",
			263 => "Zimbabwe"
		);
		$i=1;
		foreach($countryCallingCodes as $key=>$value)
		{
			$countrydata=array(
				"phone_code"=>$key
			);
			$this->general_model->update('country',$countrydata,"name = '".$value."'");
			echo "updated ".$i." row successfully <br>";	
			$i++;
		}
	}
	public function states()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'States'));
		$data['totalstates']=$this->general_model->select("zone","*","country_id ='223'");
		$count=count($data['totalstates']);
		$perpage = 10;
		if ($count > $perpage) {
			$this->load->library('pagination');
		    $data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';
			// Create Pagination
			$config['base_url'] = site_url(ADMIN_PATH . '/setting/states/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage; 
			$config['uri_segment'] = '4';
			$config['num_links'] = 4 ;
			$config['full_tag_open'] = false;
			$config['full_tag_close'] = false;
			$config['cur_tag_open'] = '<a href="#">';
			$config['cur_tag_close'] = '</a>';
			$config['next_tag_open'] = false;
			$config['next_tag_close'] = false;
			$config['prev_tag_open'] = false;
			$config['prev_tag_close'] = false;
			$config['num_tag_open'] = false;
			$config['num_tag_close'] = false;
			$config['suffix'] = $data['query_string'];
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(4);
		}
		$states = $this->general_model->select_rows("zone","*","zone_id","asc",$perpage,$offset,"country_id = 223");
		foreach($states as $state)
		{
			$country=$this->general_model->select_val('country','*','country_id ='.$state['country_id']);
			$data['states'][]=array(
							'stateid'=>$state['zone_id'],
							'country_id'=>$state['country_id'],
							'country_name'=>$country['name'],
							'state_name'=>$state['name'],
							'code'=>$state['code'],
							'status'=>$state['status']
							);
		}
		$this->template->content->view('admin/system/states', $data);
		$this->template->publish();
	}
	public function addstate()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/states' => 'All States', current_url() => 'Add State'));
		$this->form_validation->set_rules('name', 'State Name', 'required');
        $this->form_validation->set_rules('code', 'State Code', 'trim|required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$statedata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"code"=>$this->input->post('code'),
					"status"=>$this->input->post('status'),
				);
				$countryid=$this->general_model->insert('zone',$statedata);
				$this->session->set_flashdata('message', '<p class="success">State added successfully</p>');
				redirect(ADMIN_PATH . '/setting/states');
			}
		$data['countries']=$this->general_model->select('country',"*","country_id = 223");
		$this->template->content->view('admin/system/addstate', $data);
		$this->template->publish();
	}
	public function editstate()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/states' => 'All states', current_url() => 'Edit State'));
		$stateid=$this->uri->segment(4);
		$this->form_validation->set_rules('name', 'State Name', 'required');
        $this->form_validation->set_rules('code', 'State Code', 'trim|required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$statedata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"code"=>$this->input->post('code'),
					"status"=>$this->input->post('status'),
				);
				$this->general_model->update('zone',$statedata,"zone_id = ".$stateid);
				$this->session->set_flashdata('message', '<p class="success">State updated successfully</p>');
				redirect(ADMIN_PATH . '/setting/states');
			}
		$data['countries']=$this->general_model->select('country',"*","country_id = 223");
		$data['states']=$this->general_model->select_val("zone","*","zone_id = ".$stateid);
		$this->template->content->view('admin/system/editstate',$data);
		$this->template->publish();
	}
	public function cities()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Cities'));
		$data['totalcities']=$this->general_model->select("city");
		$count=count($data['totalcities']);
		$perpage = 10;
		if ($count > $perpage) {
			$this->load->library('pagination');
		    $data['query_string'] = ( ! empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';
			// Create Pagination
			$config['base_url'] = site_url(ADMIN_PATH . '/setting/cities/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage; 
			$config['uri_segment'] = '4';
			$config['num_links'] = 4 ;
			$config['full_tag_open'] = false;
			$config['full_tag_close'] = false;
			$config['cur_tag_open'] = '<a href="#">';
			$config['cur_tag_close'] = '</a>';
			$config['next_tag_open'] = false;
			$config['next_tag_close'] = false;
			$config['prev_tag_open'] = false;
			$config['prev_tag_close'] = false;
			$config['num_tag_open'] = false;
			$config['num_tag_close'] = false;
			$config['suffix'] = $data['query_string'];
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(4);
		}
		else
		{	$data['pagination']="";
			$offset = 0;
		}
		$cities = $this->general_model->select_rows("city","*","city_id","asc",$perpage,$offset);
		$data['cities']=array();
		foreach($cities as $city)
		{
			$country=$this->general_model->select_val('country','*','country_id ='.$city['country_id']);
			$state=$this->general_model->select_val('zone','*','zone_id ='.$city['state_id']);
			$data['cities'][]=array(
							'cityid'=>$city['city_id'],
							'country_name'=>$country['name'],
							'state_name'=>$state['name'],
							'city_name'=>$city['name'],
							'status'=>$city['status']
							);
		}
		$this->template->content->view('admin/system/cities', $data);
		$this->template->publish();
	}
	public function addcity()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/cities' => 'All Cities', current_url() => 'Add City'));
		$this->form_validation->set_rules('name', 'City Name', 'required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$citydata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"state_id"=>$this->input->post('state_id'),
					"status"=>$this->input->post('status'),
				);
				$citytd=$this->general_model->insert('city',$citydata);
				$this->session->set_flashdata('message', '<p class="success">City added successfully</p>');
				redirect(ADMIN_PATH . '/setting/cities');
			}
		$data['countries']=$this->general_model->select('country');
		$this->template->content->view('admin/system/addcity', $data);
		$this->template->publish();
	}
	public function editcity()
	{
		$data['breadcrumb'] = set_crumbs(array('setting/cities' => 'All City', current_url() => 'Edit City'));
		$cityid=$this->uri->segment(4);
		$this->form_validation->set_rules('name', 'State Name', 'required');
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$citydata=array(
					"name"=>$this->input->post('name'),
					"country_id"=>$this->input->post('country_id'),
					"state_id"=>$this->input->post('state_id'),
					"status"=>$this->input->post('status'),
				);
				$this->general_model->update('city',$citydata,"city_id = ".$cityid);
				$this->session->set_flashdata('message', '<p class="success">City updated successfully</p>');
				redirect(ADMIN_PATH . '/setting/cities');
			}
		$data['countries']=$this->general_model->select('country');
		$data['city']=$this->general_model->select_val("city","*","city_id = ".$cityid);
		$this->template->content->view('admin/system/editcity',$data);
		$this->template->publish();
	}
	public function updatecity()
	{
		
	}
	public function server_info()
	{
		$data = array();
        $data['breadcrumb'] = set_crumbs(array(current_url() => 'Server Info'));
        $this->template->stylesheet->add('/assets/admin/css/server_info.css');
        ob_start();
        phpinfo();
        $pinfo = ob_get_contents();
        ob_end_clean();
        $data['pinfo'] = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);
        $this->template->content->view('admin/system/server_info', $data);
		$this->template->publish();
	}
}